package com.wswy.wzcx.widget

import android.content.Context
import android.support.v4.app.FragmentActivity
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.che.common.map.LocationModel
import com.che.libcommon.utils.ToastUtils
import com.google.android.flexbox.FlexboxLayout
import com.wswy.wzcx.R
import com.wswy.wzcx.model.ICity
import com.wswy.wzcx.module.GlobalConfigManager
import com.wswy.wzcx.module.LocManager
import kotlinx.android.synthetic.main.layout_hot_city.view.*

/**
 * 热门城市和 当前位置
 */
class HotCityLayout : LinearLayout,View.OnClickListener {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

        View.inflate(context, R.layout.layout_hot_city,this)


    }

    var topHotClickListener : ((ICity)->Unit)?=null
    var useLocalFind = false

    override fun onFinishInflate() {
        super.onFinishInflate()

        tv_current_city.setOnClickListener(this)
        ContextCompat.getDrawable(context,R.drawable.ic_pin)?.let {

            it.setBounds(0,0,SizeUtils.dp2px(16f),SizeUtils.dp2px(16f))
            tv_current_city.setCompoundDrawables(it,null,null,null)
        }
    }

    private var allCityList:List<ICity>?=null

    fun setHotCity(hotList:List<ICity>,allList:List<ICity>?){


        allCityList = allList

        val flexboxLayout = findViewById<FlexboxLayout>(R.id.hot_city);

        val colors = ContextCompat.getColorStateList(context,R.color.press_textview_color)
        val max = 3
        hotList.forEachIndexed { index, city ->

            TextView(context).apply {
                textSize = 14f
                text = city.cityName

                setTextColor(colors)
                setBackgroundResource(R.drawable.btn_choose_city_bg)
                gravity = Gravity.CENTER

                isClickable = true
                tag = city
                setOnClickListener(this@HotCityLayout)

            }.run {
                FlexboxLayout.LayoutParams(0, SizeUtils.dp2px(34f)).apply {

                    flexGrow = 1f

                    rightMargin = SizeUtils.dp2px(10f)
                    if (index >= max){

                        topMargin = SizeUtils.dp2px(10f)

                        if (index % max == 0){
                            isWrapBefore = true
                        }
                    }


                }.let {

                    flexboxLayout.addView(this,it)

                }

            }

        }


        loadCurrentCity()
    }



    private fun loadCurrentCity(){

        LocManager.getInstance().run {

            if (hasPermission()){
                currentLocation?.let {
                    parseCityList(it)
                }
            }else{
                tv_current_city.text = "定位中"
                (context as? FragmentActivity)?.let {

                    requestAndLoad(it,object : LocManager.OnRequestLocationCallback{
                        override fun onGetLocation(locationModel: LocationModel?) {
                            if (locationModel != null){
                                parseCityList(locationModel)
                            }

                        }

                        override fun onError(msg: String?) {
                            tv_current_city.text = "定位失败"
                            tv_current_city.tag = null
                            ToastUtils.showText(msg)
                        }

                    })

                }

            }
        }
    }

    private fun parseCityList(locationModel: LocationModel){


        if (TextUtils.isEmpty(locationModel.city)){
            tv_current_city.tag = null
        }else{
            //本地匹配
            if (useLocalFind){
                GlobalConfigManager.getInstance().cityManager.findByAdCode(locationModel.adCode)?.let {
                    tv_current_city.text = it.cityName
                    tv_current_city.tag = it
                }
            }else{
                //城市选择 或估值城市
                allCityList?.forEach {
                    if (TextUtils.equals(locationModel.city,it.cityName)){
                        tv_current_city.text = locationModel.city
                        tv_current_city.tag = it
                        return@forEach
                    }
                }
            }

        }

    }


    override fun onClick(v: View?) {
        when(v?.id){


            R.id.tv_current_city -> {

                (v.tag as? ICity)?.let {
                    topHotClickListener?.invoke(it)
                }?:kotlin.run {
                    loadCurrentCity()
                }

            }

            else -> {
                //热门城市
                (v?.tag as? ICity)?.let {
                    topHotClickListener?.invoke(it)
                }
            }
        }
    }


}